package com.yonyou.framework.library.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * 客户端日期工具类 提供常用时间日期处理
 * 
 * @author joe
 * @version 2015.03.26
 * 
 */
public class AppDateUtil {

	/**
	 * 日期格式：yyyy-MM-dd
	 */
	public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

	/**
	 * 日期格式：yyyyMMdd
	 */
	public static final String YYYYMMDD = "yyyyMMdd";
	/**
	 * 日期格式：yyyy-MM-dd
	 */
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	/**
	 * 日期格式：yy-MM-dd
	 */
	public static final String YY_MM_DD = "yy-MM-dd";
	
	 /**
   * 日期格式：HH:mm
   */
  public static final String HH_MM = "HH:mm";
	/**
	 * 日期格式：MM/dd HH:mm:ss
	 */
	public static final String MM_DD_HH_MM_SS = "MM/dd HH:mm:ss";
	/**
	 * 日期格式：yyyy-MM-dd HH:mm
	 */
	public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

	/**
	 * 日期格式：yyyy-MM-dd HH:mm:ss
	 */
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	/**
	 * 日期格式：yyyy-MM-ddHH:mm:ss
	 */
	public static final String YYYY_MM_DDHH_MM_SS = "yyyy-MM-ddHH:mm:ss";
	/**
	 * 日期格式：MM-dd HH:mm
	 */
	public static final String MM_DD_HH_MM = "MM-dd HH:mm";

	/**
	 * 日期格式：MM-dd
	 */
	public static final String MM_DD = "MM-dd";

	/**
	 * 日期格式：MM月 dd日 HH:mm
	 */
	public static final String MM_DD1 = "MM月dd日";
	/**
	 * 日期格式：MM月 dd日 HH:mm
	 */
	public static final String MM_DD_HH_MM1 = "MM月dd日 HH:mm";

	/**
	 * 日期格式：YY年 MM月 dd日 HH:mm
	 */
	public static final String YYYY_MM_DD_HH_MM1 = "yyyy年MM月dd日 HH:mm";

	/**
	 * 日期格式：YY年 MM月 dd日
	 */
	public static final String YYYY_MM_DD1 = "yyyy年MM月dd日";

	/**
	 * 根据long值，转化为yyyy-MM-dd hh:mm的格式
	 * 
	 * @param timelong
	 * @return String
	 */
	public static String getTimeStamp(long timelong) {
		Date d = new Date(timelong);
		SimpleDateFormat df = new SimpleDateFormat(YYYY_MM_DD_HH_MM);
		return df.format(d);
	}

	/**
	 * timepatten就是SimpleDateFormat用到的patten，比如："yyyy-MM-dd HH:mm"
	 * 
	 * @param timelong
	 * @param timepatten
	 * @return String
	 */
	public static String getTimeStamp(long timelong, String timepatten) {
		Date d = new Date(timelong);
		SimpleDateFormat df = new SimpleDateFormat(timepatten);
		return df.format(d);
	}
	/**
	 * timepatten就是SimpleDateFormat用到的patten，比如："yyyy-MM-dd HH:mm"
	 *
	 * @param timelong
	 * @param timepatten
	 * @return String
	 */
	public static String getTimeStamp(Long timelong, String timepatten) {
		if(null==timelong){
			return "";
		}
		Date d = new Date(timelong);
		SimpleDateFormat df = new SimpleDateFormat(timepatten);
		return df.format(d);
	}
	/**
	 * timepatten就是SimpleDateFormat用到的patten，比如："yyyy-MM-dd HH:mm"
	 * 
	 * @param date
	 * @param timepatten
	 * @return String
	 */
	public static String getTimeStamp(Date date, String timepatten) {
		SimpleDateFormat df = new SimpleDateFormat(timepatten);
		return df.format(date);
	}

	/**
	 * 根据年月日的值取得时间片
	 * 
	 * @param mYear
	 * @param mMonth
	 * @param mDay
	 * @return long
	 */
	public static long getTimeStampFromYearMonthDay(int mYear, int mMonth,
			int mDay) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, mYear);
		calendar.set(Calendar.MONTH, mMonth);
		calendar.set(Calendar.DAY_OF_MONTH, mDay);
		return calendar.getTimeInMillis();
	}

	/**
	 * 解析字符串，返回Date对象，字符串格式默认为yyyy-MM-dd；如果解析出现异常，返回null。
	 * 
	 * @param strDate
	 * @return Date
	 */
	public static Date parseDate(String strDate) {
		return parseDate(strDate, YYYY_MM_DD);
	}

	/**
	 * 解析字符串，返回Date对象，可以传入字符串格式进行解析，建议使用DateUtil内置的字符串格式。如果解析出现异常，返回null。
	 * 
	 * @param strDate
	 * @return Date
	 */
	public static Date parseDate(String strDate, String dateFormat) {
		Date date = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
			date = sdf.parse(strDate);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/**
	 * 根据年月日的值取得时间片
	 * 
	 * @param mYear
	 * @param mMonth
	 * @param mDay
	 * @return long
	 */
	public static long getTimeStampFromYearMonthDay(int mYear, int mMonth,
			int mDay, int mHour, int mMin) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, mYear);
		calendar.set(Calendar.MONTH, mMonth);
		calendar.set(Calendar.DAY_OF_MONTH, mDay);
		calendar.set(Calendar.HOUR, mHour);
		calendar.set(Calendar.MINUTE, mMin);
		return calendar.getTimeInMillis();
	}

	/**
	 * 比较传入时间是否比当前小
	 * @param year
	 * @param month
	 * @param dayOfMonth
	 * @return
	 */
	public static boolean compareDateIsLessThanNow(int year, int month,
			int dayOfMonth) {
		Calendar calendar = Calendar.getInstance();
		int nowYear = calendar.get(Calendar.YEAR);
		int nowMonth = calendar.get(Calendar.MONTH);
		int nowDayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
		if (year < nowYear) {
			return true;
		}
		if (nowYear > year) {
			return false;
		}
		if (month < nowMonth) {
			return true;
		}
		if (nowMonth > month) {
			return false;
		}
		if (dayOfMonth < nowDayOfMonth) {
			return true;
		}
		return false;
	}

	/**
	 *获取日期的开始时间
	 * @param date
     */
	public static long getDayStartTime(Date date){
		if (null==date){
			date = new Date();
		}
		//设置系统时区为北京时区，否则会导致时间和服务器时间不一致
		Calendar startCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
		startCalendar.setTime(date);
		startCalendar.set(Calendar.HOUR_OF_DAY, 0);
		startCalendar.set(Calendar.MINUTE, 0);
		startCalendar.set(Calendar.SECOND, 0);
		startCalendar.set(Calendar.MILLISECOND, 1);
		return startCalendar.getTimeInMillis();
	}

	/**
	 * 获取日期的截止时间
	 * @param date
	 * @return
     */
	public static long getDayEndTime(Date date){
		if (null==date){
			date = new Date();
		}
		//设置系统时区为北京时区，否则会导致时间和服务器时间不一致
		Calendar endCalendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+8"));
		endCalendar.setTime(date);
		endCalendar.set(Calendar.HOUR_OF_DAY, 23);
		endCalendar.set(Calendar.MINUTE, 59);
		endCalendar.set(Calendar.SECOND, 59);
		endCalendar.set(Calendar.MILLISECOND, 999);
		return endCalendar.getTimeInMillis();
	}

	/**
	 * 判断是否是同一天
	 * @param date1
	 * @param date2
     * @return
     */
	public static boolean isSameDay(Date date1, Date date2) {
		Calendar calDateA = Calendar.getInstance();
		calDateA.setTime(date1);

		Calendar calDateB = Calendar.getInstance();
		calDateB.setTime(date2);

		return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
				&& calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
				&& calDateA.get(Calendar.DAY_OF_MONTH) == calDateB
				.get(Calendar.DAY_OF_MONTH);
	}
	/**
	 * 判断是否是同一天
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean isSameDay(Calendar date1, Calendar date2) {
		if (null==date1||date2==null){
			return false;
		}
		Calendar calDateA = date1;
		Calendar calDateB = date2;
		return calDateA.get(Calendar.YEAR) == calDateB.get(Calendar.YEAR)
				&& calDateA.get(Calendar.MONTH) == calDateB.get(Calendar.MONTH)
				&& calDateA.get(Calendar.DAY_OF_MONTH) == calDateB
				.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 是不是周末
	 * @param date
	 * @return
	 */
	public static boolean isWeekend(Date date) {
		if (null==date){
			return false;
		}
		Calendar calDateA = Calendar.getInstance();
		calDateA.setTime(date);
		return calDateA.get(Calendar.DAY_OF_WEEK)==Calendar.SATURDAY||calDateA.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY;
	}

	public static String getDateToString(long milSecond, String pattern) {
		Date date = new Date(milSecond);
		SimpleDateFormat format = new SimpleDateFormat(pattern);
		return format.format(date);
	}

	public static long getStringToDate(String dateString, String pattern) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		Date date = new Date();
		try{
			date = dateFormat.parse(dateString);
		} catch(ParseException e) {
			e.printStackTrace();
		}
		return date.getTime();
	}

}
